package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Order;
import org.csu.mypetstore.service.LogService;
import org.csu.mypetstore.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class MyOrdersFormServlet extends HttpServlet {
    private static final String MY_ORDERS_FORM = "/WEB-INF/jsp/order/ListOrders.jsp";
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = new OrderService();
        HttpSession session = req.getSession();
        Account account = (Account)session.getAttribute("account");

        List<Order> orders = orderService.getOrdersByUsername(account.getUsername());

        session.setAttribute("orders",orders);


        if(account != null){
            HttpServletRequest httpRequest= req;
            String strBackUrl = "http://" + req.getServerName() + ":" + req.getServerPort()
                    + httpRequest.getContextPath() + httpRequest.getServletPath() + "?" + (httpRequest.getQueryString());

            LogService logService = new LogService();

            String logInfo = logService.logInfo(" ") + strBackUrl + account.getUsername()+" 观看自己的订单信息";
            logService.insertLogInfo(account.getUsername(), logInfo);
        }

        req.getRequestDispatcher(MY_ORDERS_FORM).forward(req,resp);
    }
}
